var getCompetitionList = function(callback){
	$.get('/s/competitions/list', callback);
};

var cleanUpCompetitionData = function(rawData){
	if(rawData){
		var data = $.map(rawData, function(v,i){
			v.fields.id = v.pk;
			v.fields.referee = v.fields.referee.fields;
			return v.fields;
		});
		return data;
	}
};


var refreshGrid = function(){
	getCompetitionList(function(data){
		var dataList = cleanUpCompetitionData(data);
		var grid = getGrid();
		var ds = new kendo.data.DataSource({data:dataList})
		grid.setDataSource(ds);
		grid.refresh();				
	})
};

onEditCompetition = function(e){
	e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var record = this.dataSource.getByUid(dataItem.uid);
    window.location.href = "/s/competitions/edit?id="+ record.key;
}

onUmpiresInvitation = function(e){
	e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var record = this.dataSource.getByUid(dataItem.uid);
    window.location.href = "/s/workflow?page=inviteUmpires&id="+ record.key;
}

onChooseUmpires = function(e) {
	e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var record = this.dataSource.getByUid(dataItem.uid);
    window.location.href = "/s/workflow?page=chooseUmpires&id=" + record.key ;
}

$(function(){
	getCompetitionList(function(data){
		
		var dataList = (data);
		
		//setup grid for data
		var grid = $("#grid").kendoGrid({
	        dataSource: dataList,
	        sortable: true,
	        resizable: true,
	        toolbar: [{template: kendo.template($('#toolbar-competition').html())}],
	        selectable: "multiple",
	        pageable: {
	            refresh: true,
	            pageSizes: true
	        },
	        columns: [ {
	                field: "name",
	                width: 130,
	                title: "Name"
	            } , {
	                field: "startDate",
	                width: 100,
	                title: "Start Date"
	            } , {
	                width: 100,
	                field: "endDate",
	                title: "End Date"
	            } , {          
	            	width: 130,
	                field: "location",
	                title: "Location"
	            } , {	       
	            	width: 130,
	                field: "refereeName",
	                title: "Referee",
	            } , {
	            	command : [{name : 'Edit', click : onEditCompetition},
	            	           {name : 'Invite Umpires', click : onUmpiresInvitation},
	            	           {name : 'Choose Umpires', click : onChooseUmpires}
	            	]
	            }
	        ]
	    });
		
		//setup button for toolbar
		$('#btn-new').click(function(e){
			window.location.href = '/s/competitions/new';
		});
		
		$('#btn-deleteSelected').click(function(e){			
			
			var grid = $('#grid').data('kendoGrid');
			var selectedRows = grid.select();
			if(selectedRows){
								
				var csrftoken = getCookie('csrftoken');
				
				var idsToBeDeleted = $.map(selectedRows, function(k,i){
					var data =grid.dataItem(k);
					return data.key;
				});
				
				
				$.ajax({url:'/s/competitions/delete', 
						data : {keys: idsToBeDeleted.join(",")},
						type: 'POST',
						beforeSend: function(request){
							request.setRequestHeader("X-CSRFToken", csrftoken);				         
						},
						success: function(data){
							refreshGrid();							
						}
				});		
				
			}
			
		});
		
		$('#btn-exportCsv').click(function(e){
			$.get('/s/competitions/export', function(){
				
			});
		});
		
	});
	
});